
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.2.3, mkdocs-material-7.3.6">
    
    
      
        <title>测试用例组织 - HttpRunner V3.x中文文档</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.a57b2b03.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.3f5d1f46.min.css">
        
      
    
    
    
      
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>:root{--md-text-font-family:"Roboto";--md-code-font-family:"Roboto Mono"}</style>
      
    
    
    
    
      


    
    
  </head>
  
  
    
    
    
    
    
    <body dir="ltr" data-md-color-scheme="" data-md-color-primary="none" data-md-color-accent="none">
  
    
    <script>function __prefix(e){return new URL("../..",location).pathname+"."+e}function __get(e,t=localStorage){return JSON.parse(t.getItem(__prefix(e)))}</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-header__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            HttpRunner V3.x中文文档
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              测试用例组织
            
          </span>
        </div>
      </div>
    </div>
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
      </label>
      
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-nav__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    HttpRunner V3.x中文文档
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        名词解释
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../10-HttpRunner%E7%AE%80%E4%BB%8B/" class="md-nav__link">
        简介
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../20-HttpRunner%E5%AE%89%E8%A3%85/" class="md-nav__link">
        安装
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_4" type="checkbox" id="__nav_4" >
      
      
      
      
        <label class="md-nav__link" for="__nav_4">
          用户指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="用户指南" data-md-level="1">
        <label class="md-nav__title" for="__nav_4">
          <span class="md-nav__icon md-icon"></span>
          用户指南
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/31-%E6%A6%82%E8%BF%B0/" class="md-nav__link">
        HttpRunner概念
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/32-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" class="md-nav__link">
        快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/33-%E5%BD%95%E5%88%B6%E5%B9%B6%E7%94%9F%E6%88%90%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-nav__link">
        录制并生成测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/34-%E7%BC%96%E5%86%99%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-nav__link">
        编写测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/35-%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-nav__link">
        运行测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/36-%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-nav__link">
        测试报告
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/37-%E8%BF%90%E8%A1%8C%E8%B4%9F%E8%BD%BD%E6%B5%8B%E8%AF%95/" class="md-nav__link">
        运行负载测试
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/39-%E5%8F%91%E5%B8%83%E5%8E%86%E5%8F%B2/" class="md-nav__link">
        发布历史
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_5" type="checkbox" id="__nav_5" checked>
      
      
      
      
        <label class="md-nav__link" for="__nav_5">
          补充
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="补充" data-md-level="1">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          补充
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../10-%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E7%BB%84%E7%BB%87/" class="md-nav__link">
        项目文件组织
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          测试用例组织
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        测试用例组织
      </a>
      
        


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#yaml-json" class="md-nav__link">
    YAML &amp; JSON
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    测试用例结构
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#context" class="md-nav__link">
    变量空间（context）作用域
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#config" class="md-nav__link">
    config
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#test" class="md-nav__link">
    test
  </a>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../30-Hook%E6%9C%BA%E5%88%B6/" class="md-nav__link">
        Hook机制
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../40-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/" class="md-nav__link">
        环境变量
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../50-%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E5%88%86%E5%B1%82/" class="md-nav__link">
        测试用例分层
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../60-%E5%8F%82%E6%95%B0%E5%8C%96%E6%95%B0%E6%8D%AE%E9%A9%B1%E5%8A%A8/" class="md-nav__link">
        参数化数据驱动
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../70-%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9C%BA%E6%99%AF/" class="md-nav__link">
        文件上传场景
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#yaml-json" class="md-nav__link">
    YAML &amp; JSON
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    测试用例结构
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#context" class="md-nav__link">
    变量空间（context）作用域
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#config" class="md-nav__link">
    config
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#test" class="md-nav__link">
    test
  </a>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                
                
                <h1 id="_1">测试用例组织</h1>
<h2 id="yaml-json">YAML &amp; JSON</h2>
<p>HttpRunner 的测试用例支持两种文件格式：YAML 和 JSON。</p>
<p>JSON 和 YAML 格式的测试用例完全等价，包含的信息内容也完全相同。</p>
<ul>
<li>对于新手来说，推荐使用 JSON 格式，虽然描述形式上稍显累赘，但是不容易出错（大多编辑器都具有 JSON 格式的检测功能）；同时，HttpRunner 也内置了 JSON 格式正确性检测和样式美化功能，详情可查看<a href="https://v2.httprunner.org/testcase//validate-pretty.md">《Validate &amp; Prettify》</a>。</li>
<li>对于熟悉 YAML 格式的人来说，编写维护 YAML 格式的测试用例会更简洁，但前提是要保证 YAML 格式没有语法错误。</li>
</ul>
<p>对于两种格式的展示差异，可以对比查看 <a href="https://v2.httprunner.org/data/demo-quickstart-6.json">demo-quickstart-6.json</a> 和 <a href="https://v2.httprunner.org/data/demo-quickstart-6.yml">demo-quickstart-6.yml</a> 获取初步的印象。</p>
<p>后面为了更清晰的描述，统一采用 JSON 格式作为示例。</p>
<h2 id="_2">测试用例结构</h2>
<p><img alt="testcase-structure" src="https://v2.httprunner.org/images/testcase-structure.png" /></p>
<p>在 HttpRunner 中，测试用例组织主要基于三个概念：</p>
<ul>
<li>测试用例集（testsuite）：对应一个文件夹，包含单个或多个测试用例（<code>YAML/JSON</code>）文件</li>
<li>测试用例（testcase）：对应一个 <code>YAML/JSON</code> 文件，包含单个或多个测试步骤</li>
<li>测试步骤（teststep）：对应 <code>YAML/JSON</code> 文件中的一个 <code>test</code>，描述单次接口测试的全部内容，包括发起接口请求、解析响应结果、校验结果等</li>
</ul>
<p>对于单个 <code>YAML/JSON</code> 文件来说，数据存储结构为 <code>list of dict</code> 的形式，其中可能包含一个全局配置项（config）和若干个测试步骤（test）。</p>
<p>具体地：</p>
<ul>
<li>config：作为整个测试用例的全局配置项</li>
<li>test：对应单个测试步骤（teststep），测试用例存在顺序关系，运行时将从前往后依次运行各个测试步骤</li>
</ul>
<p>对应的 JSON 格式如下所示：</p>
<pre><code class="language-json">[
  {
    &quot;config&quot;: {...}
  },
  {
    &quot;test&quot;: {...}
  },
  {
    &quot;test&quot;: {...}
  }
]
</code></pre>
<h2 id="context">变量空间（context）作用域</h2>
<p>在测试用例内部，HttpRunner 划分了两层变量空间作用域（context）。</p>
<ul>
<li>config：作为整个测试用例的全局配置项，作用域为整个测试用例；</li>
<li>test：测试步骤的变量空间（context）会继承或覆盖 config 中定义的内容；</li>
<li>若某变量在 config 中定义了，在某 test 中没有定义，则该 test 会继承该变量</li>
<li>若某变量在 config 和某 test 中都定义了，则该 test 中使用自己定义的变量值</li>
<li>各个测试步骤（test）的变量空间相互独立，互不影响；</li>
<li>如需在多个测试步骤（test）中传递参数值，则需要使用 extract 关键字，并且只能从前往后传递</li>
</ul>
<h2 id="config">config</h2>
<pre><code class="language-yaml">&quot;config&quot;: {
    &quot;name&quot;: &quot;testcase description&quot;,
    &quot;parameters&quot;: [
        {&quot;user_agent&quot;: [&quot;iOS/10.1&quot;, &quot;iOS/10.2&quot;, &quot;iOS/10.3&quot;]},
        {&quot;app_version&quot;: &quot;${P(app_version.csv)}&quot;},
        {&quot;os_platform&quot;: &quot;${get_os_platform()}&quot;}
    ],
    &quot;variables&quot;: [
        {&quot;user_agent&quot;: &quot;iOS/10.3&quot;},
        {&quot;device_sn&quot;: &quot;${gen_random_string(15)}&quot;},
        {&quot;os_platform&quot;: &quot;ios&quot;}
    ],
    &quot;request&quot;: {
        &quot;base_url&quot;: &quot;http://127.0.0.1:5000&quot;,
        &quot;headers&quot;: {
            &quot;Content-Type&quot;: &quot;application/json&quot;,
            &quot;device_sn&quot;: &quot;$device_sn&quot;
        }
    },
    &quot;output&quot;: [
        &quot;token&quot;
    ]
}
</code></pre>
<table>
<thead>
<tr>
<th align="left">Key</th>
<th align="left">required?</th>
<th align="left">format</th>
<th align="left">descrption</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">name</td>
<td align="left">Yes</td>
<td align="left">string</td>
<td align="left">测试用例的名称，在测试报告中将作为标题</td>
</tr>
<tr>
<td align="left">variables</td>
<td align="left">No</td>
<td align="left">list of dict</td>
<td align="left">定义的全局变量，作用域为整个用例</td>
</tr>
<tr>
<td align="left">parameters</td>
<td align="left">No</td>
<td align="left">list of dict</td>
<td align="left">全局参数，用于实现数据化驱动，作用域为整个用例</td>
</tr>
<tr>
<td align="left">request</td>
<td align="left">No</td>
<td align="left">dict</td>
<td align="left">request 的公共参数，作用域为整个用例；常用参数包括 base_url 和 headers</td>
</tr>
</tbody>
</table>
<p><strong>request</strong></p>
<table>
<thead>
<tr>
<th align="left">Key</th>
<th align="left">required?</th>
<th align="left">format</th>
<th align="left">descrption</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">base_url</td>
<td align="left">No</td>
<td align="left">string</td>
<td align="left">测试用例请求 URL 的公共 host，指定该参数后，test 中的 url 可以只描述 path 部分</td>
</tr>
<tr>
<td align="left">headers</td>
<td align="left">No</td>
<td align="left">dict</td>
<td align="left">request 中 headers 的公共参数，作用域为整个用例</td>
</tr>
<tr>
<td align="left">output</td>
<td align="left">No</td>
<td align="left">list</td>
<td align="left">整个用例输出的参数列表，可输出的参数包括公共的 variable 和 extract 的参数; 在 log-level 为 debug 模式下，会在 terminal 中打印出参数内容</td>
</tr>
</tbody>
</table>
<h2 id="test">test</h2>
<pre><code class="language-json">&quot;test&quot;: {
    &quot;name&quot;: &quot;get token with $user_agent, $os_platform, $app_version&quot;,
    &quot;request&quot;: {
        &quot;url&quot;: &quot;/api/get-token&quot;,
        &quot;method&quot;: &quot;POST&quot;,
        &quot;headers&quot;: {
            &quot;app_version&quot;: &quot;$app_version&quot;,
            &quot;os_platform&quot;: &quot;$os_platform&quot;,
            &quot;user_agent&quot;: &quot;$user_agent&quot;
        },
        &quot;json&quot;: {
            &quot;sign&quot;: &quot;${get_sign($user_agent, $device_sn, $os_platform, $app_version)}&quot;
        },
        &quot;extract&quot;: [
            {&quot;token&quot;: &quot;content.token&quot;}
        ],
        &quot;validate&quot;: [
            {&quot;eq&quot;: [&quot;status_code&quot;, 200]},
            {&quot;eq&quot;: [&quot;headers.Content-Type&quot;, &quot;application/json&quot;]},
            {&quot;eq&quot;: [&quot;content.success&quot;, true]}
        ],
        &quot;setup_hooks&quot;: [],
        &quot;teardown_hooks&quot;: []
    }
}
</code></pre>
<table>
<thead>
<tr>
<th align="left">Key</th>
<th align="left">required?</th>
<th align="left">format</th>
<th align="left">descrption</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">name</td>
<td align="left">Yes</td>
<td align="left">string</td>
<td align="left">测试步骤的名称，在测试报告中将作为测试步骤的名称</td>
</tr>
<tr>
<td align="left">request</td>
<td align="left">Yes</td>
<td align="left">dict</td>
<td align="left">HTTP 请求的详细内容；可用参数详见 <a href="http://docs.python-requests.org/en/master/api/#main-interface">python-requests</a> 官方文档</td>
</tr>
<tr>
<td align="left">variables</td>
<td align="left">No</td>
<td align="left">list of dict</td>
<td align="left">测试步骤中定义的变量，作用域为当前测试步骤</td>
</tr>
<tr>
<td align="left">extract</td>
<td align="left">No</td>
<td align="left">list</td>
<td align="left">从当前 HTTP 请求的响应结果中提取参数，并保存到参数变量中（例如<code>token</code>），后续测试用例可通过<code>$token</code>的形式进行引用</td>
</tr>
<tr>
<td align="left">validate</td>
<td align="left">No</td>
<td align="left">list</td>
<td align="left">测试用例中定义的结果校验项，作用域为当前测试用例，用于实现对当前测试用例运行结果的校验</td>
</tr>
<tr>
<td align="left">setup_hooks</td>
<td align="left">No</td>
<td align="left">list</td>
<td align="left">在 HTTP 请求发送前执行 hook 函数，主要用于准备工作</td>
</tr>
<tr>
<td align="left">teardown_hooks</td>
<td align="left">No</td>
<td align="left">list</td>
<td align="left">在 HTTP 请求发送后执行 hook 函数，主要用户测试后的清理工作</td>
</tr>
</tbody>
</table>
<p><strong>extract</strong></p>
<p>支持多种提取方式：</p>
<ul>
<li>响应结果为 JSON 结构，可采用<code>.</code>运算符的方式，例如<code>headers.Content-Type</code>、<code>content.success</code>；</li>
<li>响应结果为 text/html 结构，可采用正则表达式的方式，例如<code>blog-motto\"&gt;(.*)</code></li>
<li>详情可阅读<a href="http://debugtalk.com/post/apitestengine-not-only-about-json-api/">《ApiTestEngine，不再局限于API的测试》</a></li>
</ul>
<p><strong>validate</strong></p>
<p>支持两种格式：</p>
<ul>
<li><code>{"comparator_name": [check_item, expect_value]}</code></li>
<li><code>{"check": check_item, "comparator": comparator_name, "expect": expect_value}</code></li>
</ul>
<p><strong>hooks</strong></p>
<p>setup_hooks 函数放置于 debugtalk.py 中，并且必须包含三个参数：</p>
<ul>
<li>method: 请求方法，e.g. GET, POST, PUT</li>
<li>url: 请求 URL</li>
<li>kwargs: request 的参数字典</li>
</ul>
<p>teardown_hooks 函数放置于 debugtalk.py 中，并且必须包含一个参数：</p>
<ul>
<li>resp_obj: requests.Response 实例</li>
</ul>
<p>关于 <code>setup_hooks</code> 和 <code>teardown_hooks</code> 的更多内容，请参考<a href="https://v2.httprunner.org/prepare/request-hook/">《hook 机制》</a>。</p>
                
              
              
                


              
            </article>
          </div>
        </div>
        
      </main>
      
        
<footer class="md-footer">
  
    <nav class="md-footer__inner md-grid" aria-label="Footer">
      
        
        <a href="../10-%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E7%BB%84%E7%BB%87/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 项目文件组织" rel="prev">
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
          </div>
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                上一页
              </span>
              项目文件组织
            </div>
          </div>
        </a>
      
      
        
        <a href="../30-Hook%E6%9C%BA%E5%88%B6/" class="md-footer__link md-footer__link--next" aria-label="下一页: Hook机制" rel="next">
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                下一页
              </span>
              Hook机制
            </div>
          </div>
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg>
          </div>
        </a>
      
    </nav>
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">
        
        
          Made with
          <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
            Material for MkDocs
          </a>
        
        
      </div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    <script id="__config" type="application/json">{"base": "../..", "features": [], "translations": {"clipboard.copy": "\u590d\u5236", "clipboard.copied": "\u5df2\u590d\u5236", "search.config.lang": "ja", "search.config.pipeline": "trimmer, stemmer", "search.config.separator": "[\\uff0c\\u3002]+", "search.placeholder": "\u641c\u7d22", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing", "select.version.title": "Select version"}, "search": "../../assets/javascripts/workers/search.fcfe8b6d.min.js", "version": null}</script>
    
    
      <script src="../../assets/javascripts/bundle.b1047164.min.js"></script>
      
    
  </body>
</html>